<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :AddList="AddList" />
      <List
        :todoList="todoList"
        :singleCheckHandle="singleCheckHandle"
        :singleDeleteHandle="singleDeleteHandle"
      />
      <Footer
       :todoList='todoList' 
       :singleAllHandlell='singleAllHandlell'
       :ClearAllOver="ClearAllOver"
       />
    </div>
  </div>
</template>

<script>
import List from "./components/List/index.vue";
import Header from "./components/Header/index.vue";
import Footer from "./components/Footer";

export default {
  name: "App",
  data() {
    return {
      todoList: [
        // { id: uuidv4(), thing: "唱", node: false },
        // { id: uuidv4(), thing: "跳", node: false },
        // { id: uuidv4(), thing: "rap", node: false },
      ],
    };
  },
  components: {
    List,
    Header,
    Footer,
  },
  methods: {
    //添加数据函数
    AddList(value) {
      this.todoList.unshift(value);
    },
    //多选框逻辑
    singleCheckHandle(id) {
      this.todoList = this.todoList.map((item) => {
        return item.id === id ? { ...item, node: !item.node } : item;
      });
    },
    //删除逻辑
    singleDeleteHandle(id) {
      this.todoList = this.todoList.filter((item) => item.id !== id);
    },
    //全选逻辑
    singleAllHandlell(value){
      this.todoList = this.todoList.map(item=>{
        return {...item ,node:value}
      })
    },
    //删除完所有逻辑
    ClearAllOver(){
      this.todoList = this.todoList.filter(item=>!item.node)
    }
  },
  //增加数据流程-初始化增加数据mounted钩子函数
  //初始化挂载完成的时候 拿到localStorage的数据
  
  mounted(){
    this.todoList = JSON.parse(localStorage.getItem('todoList')||"[]")
  },

  //监听 当数据todoList变化的时候 将变化的数据放到本地中储存 
  watch:{
    todoList:{
      deep:true,
      handler(){
          localStorage.setItem('todoList',JSON.stringify(this.todoList)||"[]")
      }
    }
  }
  
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>